﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 9:25 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
using Miming.Model;
using Miming.Util;

namespace Miming.Gui
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : BaseForm
	{
		static MainForm instance = new MainForm();
		
		public static MainForm Instance {
			get { return instance; }
		}
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			AttachCommands();
			Text = ApplicationUtility.ProductNameAndVersion;
			
			int x = ConvertUtility.ToInt32(RegistryUtility.GetCurrentUserValue("X"));
			int y = ConvertUtility.ToInt32(RegistryUtility.GetCurrentUserValue("Y"));
			this.Location = new Point(x, y);
			
			int width = ConvertUtility.ToInt32(RegistryUtility.GetCurrentUserValue("Width"));
			int height = ConvertUtility.ToInt32(RegistryUtility.GetCurrentUserValue("Height"));
			this.Size = new Size(width == 0 ? 800 : width, height == 0 ? 600 : height);
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			base.OnClosing(e);
			RegistryUtility.SetCurrentUserValue("X", this.Location.X);
			RegistryUtility.SetCurrentUserValue("Y", this.Location.Y);
			RegistryUtility.SetCurrentUserValue("Width", this.Size.Width);
			RegistryUtility.SetCurrentUserValue("Height", this.Size.Height);
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.Visible = true;
			Login();
		}
		
		void Login()
		{
			menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = false;
			if (AddDialog(new LoginForm()) == DialogResult.Cancel) {
				Close();
			} else {
				menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = true;
				this.IsMdiContainer = true;
				homePageToolStripMenuItem.PerformClick();
			}
		}
		
		void AttachCommands()
		{
			logOffToolStripMenuItem.Click += delegate { LogOff(); };
			exitToolStripMenuItem.Click += delegate { Close(); };
			
			useCalculatorToolStripMenuItem.Click += delegate { System.Diagnostics.Process.Start("calc"); };
			preferencesToolStripMenuItem.Click += delegate { AddDialog(new PreferencesForm()); };
			
			chartOfAccountsToolStripMenuItem1.Click += delegate { chartOfAccountsToolStripMenuItem.PerformClick(); };
			itemListToolStripMenuItem2.Click += delegate { itemListToolStripMenuItem.PerformClick(); };
			uMSetListToolStripMenuItem.Click += delegate { AddChild(new UnitListForm()); };
			priceLevelListToolStripMenuItem.Click += delegate { AddChild(new PriceLevelListForm()); };
			billingRateLevelListToolStripMenuItem.Click += delegate { AddChild(new BillingRateLevelListForm()); };
			salesTaxCodeListToolStripMenuItem.Click += delegate { AddChild(new SalesTaxCodeListForm()); };
			classListToolStripMenuItem.Click += delegate { AddChild(new ClassListForm()); };
			workersCompListToolStripMenuItem.Click += delegate { AddChild(new WorkersCompensationListForm()); };
			otherNameListToolStripMenuItem.Click += delegate { AddChild(new OtherNameListForm()); };
			
			customerTypeListToolStripMenuItem.Click += delegate { AddChild(new CustomerTypeListForm()); };
			vendorTypeListToolStripMenuItem.Click += delegate { AddChild(new VendorTypeListForm()); };
			jobTypeListToolStripMenuItem.Click += delegate { AddChild(new JobTypeListForm()); };
			termsListToolStripMenuItem.Click += delegate { AddChild(new TermsListForm()); };
			customerMessageListToolStripMenuItem.Click += delegate { AddChild(new CustomerMessageListForm()); };
			paymentMethodListToolStripMenuItem.Click += delegate { AddChild(new PaymentMethodListForm()); };
			shipViaListToolStripMenuItem.Click += delegate { AddChild(new ShippingMethodListForm()); };
			vehicleListToolStripMenuItem.Click += delegate { AddChild(new VehicleListForm()); };
			
			homePageToolStripMenuItem.Click += delegate { AddChild(new WebBrowserForm(Path.Combine(ApplicationUtility.DocDirectory, "home.html"))); };
			companyInformationToolStripMenuItem.Click += delegate { AddDialog(new CompanyForm(CompanySingleton.Instance)); };
			setupUsersToolStripMenuItem.Click += delegate { AddDialog(new UserListForm()); };
			changePasswordToolStripMenuItem.Click += delegate { AddDialog(new ChangePasswordForm(UserSingleton.Instance)); };
			chartOfAccountsToolStripMenuItem.Click += delegate { AddChild(new AccountListForm()); };
			toDoListToolStripMenuItem.Click += delegate { AddChild(new ToDoListForm()); };
			
			createSalesOrdersToolStripMenuItem.Click += delegate { AddChild(new SalesOrderForm()); };
			createInvoicesToolStripMenuItem.Click += delegate { AddChild(new InvoiceForm()); };
			enterSalesReceiptsToolStripMenuItem.Click += delegate { AddChild(new SalesReceiptForm()); };
			customerListToolStripMenuItem.Click += delegate { AddChild(new CustomerListForm()); };
			itemListToolStripMenuItem1.Click += delegate { AddChild(new ItemListForm()); };
			
			createPurchaseOrdersToolStripMenuItem.Click += delegate { AddChild(new PurchaseOrderForm()); };
			receiveItemsToolStripMenuItem.Click += delegate { AddChild(new ItemReceiptForm()); };
			vendorListToolStripMenuItem.Click += delegate { AddChild(new VendorListForm()); };
			itemListToolStripMenuItem.Click += delegate { AddChild(new ItemListForm()); };
			purchaseOrderListToolStripMenuItem.Click += delegate { AddChild(new PurchaseOrderListForm()); };
			
			employeeListToolStripMenuItem.Click += delegate { AddChild(new EmployeeListForm()); };
			
			arrangeIconsToolStripMenuItem.Click += delegate { LayoutMdi(MdiLayout.ArrangeIcons); };
			closeAllToolStripMenuItem.Click += delegate { CloseAllChildren(); };
			tileHorizontallyToolStripMenuItem.Click += delegate { LayoutMdi(MdiLayout.TileHorizontal); };
			tileVerticallyToolStripMenuItem.Click += delegate { LayoutMdi(MdiLayout.TileVertical); };
			cascadeToolStripMenuItem.Click += delegate { LayoutMdi(MdiLayout.Cascade); };
			
			aboutToolStripMenuItem.Click += delegate { AddDialog(new AboutForm()); };
			userCommunityToolStripMenuItem.Click += delegate { System.Diagnostics.Process.Start("http://miming.codeplex.com/Thread/List.aspx"); };
			
			// Toolstrips
			toolStripButtonFeedback.Click += delegate { System.Diagnostics.Process.Start("http://miming.codeplex.com/Thread/List.aspx"); };
		}
		
		void LogOff()
		{
			CloseAllChildren(); Login();
		}
		
		void CloseAllChildren()
		{
			foreach (Form f in this.MdiChildren) {
				f.Close();
			}
		}
		
		public void AddChild(Form form, string text)
		{
			form.Text = text;
			AddChild(form);
		}
		
		public void AddChild(Form form)
		{
			form.MdiParent = this;
			form.WindowState = FormWindowState.Maximized;
			openWindowListPane1.AddForm(form);
			form.Show();
		}
		
		public DialogResult AddDialog(Form form)
		{
			form.StartPosition = FormStartPosition.CenterParent;
			form.FormBorderStyle = FormBorderStyle.FixedDialog;
			form.KeyPreview = true;
			form.MinimizeBox = form.MaximizeBox = form.ShowInTaskbar = false;
			form.KeyDown += delegate(object sender, KeyEventArgs e) { 
				if (e.KeyCode == Keys.Escape) {
					form.Close();
				}
			};
			return form.ShowDialog(this);
		}
	}
}
